<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('预算列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>类别名称：</label>
                            <select name="categoryId" class="form-control">
                                <option value="">全部</option>
                                <th:block th:each="category : ${categories}">
                                    <option th:value="${category.categoryId}" th:text="${category.categoryName}"></option>
                                </th:block>
                            </select>
                        </li>
                        <li>
                            <label>预算金额：</label>
                            <input type="text" name="budgetAmount"/>
                        </li>
                        <li>
                            <label>预算月份：</label>
                            <input type="text" class="time-input" placeholder="请选择预算月份" name="budgetMonth"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" href="javascript:void(0);" onclick="$.table.search()">
                                <i class="fa fa-search"></i>&nbsp;搜索
                            </a>
                            <a class="btn btn-warning btn-rounded btn-sm" href="javascript:void(0);" onclick="$.form.reset()">
                                <i class="fa fa-refresh"></i>&nbsp;重置
                            </a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" href="javascript:void(0);" onclick="$.operate.add()" shiro:hasPermission="system:budget:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-primary single disabled" href="javascript:void(0);" onclick="$.operate.edit()" shiro:hasPermission="system:budget:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" href="javascript:void(0);" onclick="$.operate.removeAll()" shiro:hasPermission="system:budget:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning" href="javascript:void(0);" onclick="$.table.exportExcel()" shiro:hasPermission="system:budget:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>

<script type="text/javascript">
    var editFlag = /*[[${@permission.hasPermi('system:budget:edit')}]]*/ false; // 示例默认值
    var removeFlag = /*[[${@permission.hasPermi('system:budget:remove')}]]*/ false; // 示例默认值
    var prefix = ctx + "system/budget";

    $(function() {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "预算",
            columns: [{
                checkbox: true
            },
            {
                field: 'budgetId',
                title: '预算ID',
                visible: false
            },
            {
                field: 'userId',
                title: '用户ID'
            },
            {
                field: 'categoryName',
                title: '类别名称'
            },
            {
                field: 'useAmount',
                title: '已用预算'
            },
            {
                field: 'balance', // 新增字段用于显示余额
                title: '余额',
                formatter: function(value, row, index) {
                    const balance = row.budgetAmount - row.useAmount;
                    return balance.toFixed(2);
                }
            },
            {
                field: 'budgetAmount',
                title: '预算金额'
            },
            {
                field: 'budgetMonth',
                title: '预算月份'
            },
            {
                title: '操作',
                align: 'center',
                formatter: function(value, row, index) {
                    var actions = [];
                    if (editFlag) {
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0);" onclick="$.operate.edit(\'' + row.budgetId + '\')">' +
                                    '<i class="fa fa-edit"></i>编辑</a> ');
                    }
                    if (removeFlag) {
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0);" onclick="$.operate.remove(\'' + row.budgetId + '\')">' +
                                    '<i class="fa fa-remove"></i>删除</a>');
                    }
                    return actions.join('');
                }
            }]
        };
        $.table.init(options);
    });
</script>
</body>
</html>
